﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RecipeBookBusiness;
using System.Drawing;

namespace RecipeBookWeb
{
    public partial class ViewRecipe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["RecipeID"] != null)
            {
                string recipeIDString = Request.QueryString["RecipeID"];

                try
                {
                    RecipeID = Convert.ToInt32(recipeIDString);
                }
                catch { }
            }

            if (RecipeID == -1)
            {
                BrowseRecipesPlaceHolder.Visible = true;
                IndividualRecipePlaceHolder.Visible = false;

                RecipeListView.DataSource = ModelFacade.GetAllRecipes();
                RecipeListView.DataBind();
            }
            else if (RecipeID > 0)
            {
                //Handle if the user is logged in or not
                if (Session["CurrentUser"] == null)
                {
                    RateButtonsTableRow.Visible = false;
                    AddCommentPlaceHolder.Visible = false;
                    FavoriteTableRow.Visible = false;
                }
                else
                {
                    AddCommentPlaceHolder.Visible = true;
                    RateButtonsTableRow.Visible = true;
                    FavoriteTableRow.Visible = true;
                    setRecipeButtons();
                    setFavoriteButton();
                }

                BrowseRecipesPlaceHolder.Visible = false;
                IndividualRecipePlaceHolder.Visible = true;

                DataSet recipeSet = ModelFacade.GetRecipeDataSet(RecipeID);
                if (recipeSet.Tables.IndexOf("Recipes") >= 0 && recipeSet.Tables["Recipes"].Rows.Count > 0)
                {
                    DataRow recipeDetails = recipeSet.Tables["Recipes"].Rows[0];
                    AverageRatingLabel.Text = ModelFacade.GetCompositeRating(RecipeID) + " / 7";
                    RecipeNameLabel.Text = recipeDetails["Name"].ToString();
                    ServingSizeLabel.Text = recipeDetails["ServingSize"].ToString();
                    UserAddedLabel.Text = recipeDetails["Username"].ToString();
                    DateAddedLabel.Text = ((DateTime)recipeDetails["DateAdded"]).ToShortDateString();
                    InstructionsLabel.Text = recipeDetails["Instructions"].ToString();
                    NutritionalInfoLabel.Text = recipeDetails["NutritionalInfo"].ToString();
                    if (NutritionalInfoLabel.Text.Trim().Equals(string.Empty))
                    {
                        NutritionalInfoTableRow.Visible = false;
                    }

                    IngredientGridView.DataSource = recipeSet.Tables["Ingredients"];
                    IngredientGridView.DataBind();

                    if (recipeSet.Tables["Equipment"].Rows.Count > 0)
                    {
                        EquipmentGridView.Visible = true;
                        EquipmentGridView.DataSource = recipeSet.Tables["Equipment"];
                        EquipmentGridView.DataBind();
                    }
                    else
                    {
                        EquipmentGridView.Visible = false;
                    }

                    if (recipeSet.Tables["Categories"].Rows.Count > 0)
                    {
                        CategoriesTableRow.Visible = true;
                        CategoriesLabel.Text = string.Empty;
                        foreach (DataRow dr in recipeSet.Tables["Categories"].Rows)
                        {
                            CategoriesLabel.Text += "<u>" + dr["Name"].ToString() + "</u> ";
                        }
                    }
                    else
                    {
                        CategoriesTableRow.Visible = false;
                    }

                    CommentListView.DataSource = ModelFacade.GetCommentsByRecipeID(RecipeID);
                    CommentListView.DataBind();

                    RecipeDetailTable.DataBind();
                }
            }
        }

        protected void RatingButton_Clicked(object sender, CommandEventArgs e)
        {
            int ratingValue = Convert.ToInt32(e.CommandArgument);
            if (Session["CurrentUser"] != null)
            {
                ErrorLabel.Visible = false;
                ModelFacade.AddRating(Session["CurrentUser"].ToString(), RecipeID, 0, true, ratingValue);
                setRecipeButtons();
                RateButtonsTableRow.DataBind();
                AverageRatingLabel.Text = ModelFacade.GetCompositeRating(RecipeID) + " / 7";
                AverageRatingLabel.DataBind();
            }
            else
            {
                ErrorLabel.Visible = true;
                ErrorLabel.Text = "Your Session has timed out. Please log in to rate this Recipe.<br>";
            }
        }

        protected void FavoriteButton_Clicked(object sender, CommandEventArgs e)
        {
            if (Session["CurrentUser"] != null)
            {
                ErrorLabel.Visible = false;
                if (e.CommandArgument.Equals("Add"))
                {
                    ModelFacade.AddUserFavorite(Session["CurrentUser"].ToString(), RecipeID);
                }
                else if (e.CommandArgument.Equals("Remove"))
                {
                    ModelFacade.DeleteUserFavorite(Session["CurrentUser"].ToString(), RecipeID);
                }
                setFavoriteButton();
                FavoriteTableRow.DataBind();
            }
            else
            {
                ErrorLabel.Visible = true;
                ErrorLabel.Text = "Your Session has timed out. Please log in to rate this Recipe.<br>";
            }
        }

        protected void CommentButton_Click(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] != null)
            {
                ErrorLabel.Visible = false;
                string commentText = NewCommentTextBox.Text;
                ModelFacade.AddComment(RecipeID, Session["CurrentUser"].ToString(), commentText);
                CommentListView.DataSource = ModelFacade.GetCommentsByRecipeID(RecipeID);
                CommentListView.DataBind();
                NewCommentTextBox.Text = string.Empty;
            }
            else
            {
                ErrorLabel.Visible = true;
                ErrorLabel.Text = "Your Session has timed out. Please log in to rate this Recipe.<br>";
            }
        }

        private void setFavoriteButton()
        {
            bool isFavorite = ModelFacade.IsUserFavorite(Session["CurrentUser"].ToString(), RecipeID);
            if (isFavorite)
            {
                FavoriteButton.Text = "Remove Favorite";
                FavoriteButton.CommandArgument = "Remove";
            }
            else
            {
                FavoriteButton.Text = "Add Favorite";
                FavoriteButton.CommandArgument = "Add";
            }
        }

        private void setRecipeButtons()
        {
            Button1.ForeColor = Color.Black;
            Button2.ForeColor = Color.Black;
            Button3.ForeColor = Color.Black;
            Button4.ForeColor = Color.Black;
            Button5.ForeColor = Color.Black;
            Button6.ForeColor = Color.Black;
            Button7.ForeColor = Color.Black;

            int currentRating = ModelFacade.CurrentUserRating(Session["CurrentUser"].ToString(), RecipeID);
            switch (currentRating)
            {
                case (-1):
                    break;
                case (1):
                    Button1.ForeColor = Color.Green;
                    break;
                case (2):
                    Button2.ForeColor = Color.Green;
                    break;
                case (3):
                    Button3.ForeColor = Color.Green;
                    break;
                case (4):
                    Button4.ForeColor = Color.Green;
                    break;
                case (5):
                    Button5.ForeColor = Color.Green;
                    break;
                case (6):
                    Button6.ForeColor = Color.Green;
                    break;
                case (7):
                    Button7.ForeColor = Color.Green;
                    break;
                default:
                    break;
            }
        }

        protected int RecipeID
        {
            get
            {
                if (ViewState["RecipeID"] == null)
                {
                    ViewState["RecipeID"] = -1; 
                }
                return (int)ViewState["RecipeID"];
            }
            set
            {
                ViewState["RecipeID"] = value;
            }
        }
    }
}
